from flask import Blueprint, request, jsonify
from common.models.collection import Collection
from common.models.collection_detail import CollectionDetail
from application import app,db
router_collection = Blueprint("collection", __name__)
@router_collection.route("/getList",methods = ['POST','GET'])
def getCollectionList():
    resp = {'code': 200, 'msg': "获取集合列表成功", 'data': []}
    collection = Collection.query.filter_by().all()
    # 返回id 和 name
    for coll in collection:
        tmp = {"id": coll.id, "name": coll.name}
        resp['data'].append(tmp)
    return jsonify(resp)
@router_collection.route("/getDetail",methods = ['POST','GET'])
def getCollectionDetail():
    resp = {'code': 200, 'msg': "获取集合成功", 'data': {}}
    req = request.values
    collection_id = req['collection_id'] if 'collection_id' in req else ""
    if collection_id is None:
        resp['code'] = -1
        resp['msg'] = "请求ID不符合规定"
        return jsonify(resp)
    collection = CollectionDetail.query.filter_by(collection_id=collection_id).first()
    resp['data'] = {'id': collection.id, "detial": collection.detail}
    try:
        db.session.commit()
    except:
        db.session.rollback()
    return jsonify(resp)






